package cn.stylefeng.guns.core.util;

import org.apache.http.util.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrUtil {
    /**
     * 半角转全角
     * @param input String.
     * @return 全角字符串.
     */
    public static String ToSBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                c[i] = '\u3000';                 //采用十六进制,相当于十进制的12288


            } else if (c[i] < '\177') {    //采用八进制,相当于十进制的127
                c[i] = (char) (c[i] + 65248);

            }
        }
        return new String(c);
    }

    /**
     * 全角转半角
     * @param input String.
     * @return 半角字符串
     */
    public static String ToDBC(String input) {

        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '\u3000') {
                c[i] = ' ';
            } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
                c[i] = (char) (c[i] - 65248);

            }
        }
        String returnString = new String(c);

        return returnString;
    }

    public static String getCurrentDateTime(){
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String nowString = format.format(now);
        return nowString;
    }
    public static String getTimestampToString(Object object){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示毫秒
        String str = df.format(object);
        return str;
    }

    public static boolean isMobileNO(String strMobileNO) {
        String strPattern = "^[1](3|4|5|6|7|8|9)[0-9]{9}$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strMobileNO);
        return m.matches();
    }
    public static boolean isEmpty(String value) {
        if (value == null || "".equals(value))
            return true;
        else
            return false;
    }
    public static boolean isEmpty(String ... values) {
        if (values == null || values.length==0){
            return true;
        }else{
            for(String s:values){
                if(s==null || "".equals(s)){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isNotEmpty(String value) {
        if (value == null || "".equals(value)){
            return false;
        }else{
            return true;
        }

    }
    /**
     * 判断字符串中是否包含中文
     * @param str
     * @return
     */
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomString(String str, int length) { //length表示生成字符串的长度
        String base = str;
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


    public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }

    public static String md5String(String message) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("md5");
            final byte[] md5 = md.digest(message.getBytes("utf-8"));
            return toHexString(md5);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return message;

    }

    public static byte[] toByteArray(String hexString) {

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {
            // 因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException(
                    "this byteArray must not be null or empty");

        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)// 0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

    //数组转成字符串
    public static String join(String joinStr, List datas){
        StringBuilder stringBuilder = new StringBuilder();
        if(datas==null){
            return null;
        }
        datas.forEach(item->{
            if(stringBuilder.length()==0){
                if(item instanceof String){
                    stringBuilder.append("'"+item+"'");
                }else{
                    stringBuilder.append(item+"");
                }

            }else {
                if(item instanceof String){
                    stringBuilder.append(joinStr).append("'"+item+"'");
                }else{
                    stringBuilder.append(joinStr).append(item+"");
                }

            }
        });
        return stringBuilder.toString();
    }



    public static void main(String[] args) {
        System.out.println(isMobileNO("19396360434"));
    }
}
